<template>
  <div class="page">
    <Navbar title="我要咨询" />
    <div class="content">
      <div class="form-content">
        <el-form :model="ruleForm" label-width="100px" size="small" :rules="rules" ref="ruleForm">
          <el-form-item label="咨询类型" prop="type">
            <el-select v-model="ruleForm.type" >
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="联系人" prop="contacts">
            <el-input v-model="ruleForm.contacts" />
          </el-form-item>
          <el-form-item label="联系方式" prop="mobile">
            <el-input v-model="ruleForm.mobile" />
          </el-form-item>
          <el-form-item label="问题描述" prop="content">
            <el-input v-model="ruleForm.content" type="textarea" />
          </el-form-item>
          <el-form-item label="资料上传" prop="fileList">
            <van-uploader v-model="fileList" :max-count="1" :after-read="afterRead" />
          </el-form-item>
        </el-form>
      </div>
      <el-button type="primary" style="width: 100%;margin-top: 15px" @click="onSave">确认提交</el-button>
    </div>
  </div>
</template>

<script>
import navbar from '@/mixins/navbar'
import { http_consulting, uploadFile } from '@/api'
import { Toast } from 'vant'
import { qsType } from '@/eumn'

export default {
  name: 'Consulting',
  mixins: [navbar],
  data() {
    return {
      ruleForm: {
        type: '',
        contacts: '',
        mobile: '',
        content: ''
      },
      options: qsType.options,
      fileList: [],
      fileUrl: '',
      rules: {
        type: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        contacts: [
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入问题描述', trigger: 'blur' }
        ]
      }

    }
  },
  methods: {
    afterRead(file) {
      file.status = 'uploading'
      file.message = '上传中...'
      uploadFile(file.file).then(res => {
        file.url = res.imgUrl
        file.status = 'success'
        this.fileUrl = [
          {
            fileName: res.fileName,
            fileType: res.suffix,
            fileUrl: res.imgUrl }
        ]
      }).catch((err) => {
        file.status = 'failed'
        file.message = '上传失败'
        Toast(err)
      })
    },
    onSave() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          http_consulting.create({
            ...this.ruleForm,
            file: JSON.stringify(this.fileUrl)
          }).then(res => {
            this.ruleForm = {
              type: '',
              contacts: '',
              mobile: '',
              content: ''
            }
            this.fileList = []
            Toast(res.msg)
            this.$router.back()
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.page {
    width: 100vw;
    overflow: hidden;
    .el-select,
    .el-input {
        width: 100%;
    }
    .content {
        padding: 23px 16px;
        .form-content {
            width: 100%;
            background: #FFFFFF;
            border-radius: 10px;
            padding: 27px 30px 27px 0;
        }
    }
}
</style>
